package starlingui.state.desktopState
{
	import com.imcrl.core.imcrl_internal;
	import com.imcrl.event.ListItemEvent;
	
	import flash.geom.Point;
	
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	
	import starlingui.component.AdvPageList;
	import starlingui.component.DeskList;
	import starlingui.component.Desktop;
	import starlingui.component.FolderList;
	import starlingui.component.itemRender.IconItemRender;
	
	use namespace imcrl_internal;
	public class DefaultState extends AbsState
	{
		public static const NAME:String="DefaultState";
		
		private var deskList:DeskList;
		private var folderList:FolderList;
		private var openApplicationHandle:Function;
		public function DefaultState(desktop:Desktop)
		{
			super(desktop,NAME);
		}
		
		override public function start():void{
			deskList=desktop.current as DeskList;
			folderList=desktop.folderList;
			openApplicationHandle=desktop.openApplicationHandle;
			if(openApplicationHandle !=null ){
				folderList.addEventListener(ListItemEvent.SELECTED,selectAppHandle);
			}
			deskList.addEventListener(ListItemEvent.SELECTED,selectedHandle);
			deskList.addEventListener(ListItemEvent.SELECTED_SHAKE,shakeHandle);
			desktop.scrollEnabeld=true;
		}
		
		
		private function selectAppHandle(event:ListItemEvent):void{
			var pageList:AdvPageList=event.target as AdvPageList;	
			openApplicationHandle(pageList.selectedItem.data);
		}
		
		
		override public function end():void{
			if(openApplicationHandle !=null ){
				folderList.removeEventListener(ListItemEvent.SELECTED,selectAppHandle);
			}
			deskList.removeEventListener(ListItemEvent.SELECTED,selectedHandle);
			deskList.removeEventListener(ListItemEvent.SELECTED_SHAKE,shakeHandle);
			desktop.scrollEnabeld=false;
		}
		
		protected function shakeHandle(event:ListItemEvent):void{
			desktop.shake();
		}
		
		protected function selectedHandle(event:ListItemEvent):void{
			var deskList:DeskList=event.target as DeskList;
			var item:IconItemRender=deskList.selectedItem as IconItemRender;
			desktop.selectedItem(item,deskList);
		}
	}
}